package lex.title;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.UIManager;
import lex.title.basic.PrimalWindowTitle;

public class MenuButton extends JButton {
	private Icon icon = null;
	private PrimalWindowTitle title = null;

	public MenuButton(PrimalWindowTitle title) {
		super();
		// =============================
		this.title = title;
		Image image = title.getIconImage();
		if (null == image)
			icon = UIManager.getIcon("InternalFrame.icon");
		// =============================
		this.setText(null);
		this.setBorderPainted(false);
		this.setContentAreaFilled(false);
		this.setFocusPainted(false);
		this.setFocusable(false);
		this.setOpaque(false);
		this.setRolloverEnabled(true);
		// =================================
		this.setSize(getPreferredSize());
	}

	public Dimension getMinimumSize() {
		return this.getPreferredSize();
	}

	public Dimension getPreferredSize() {
		Dimension size = new Dimension(1, 1);
		int w = 1, h = 1;
		Image img = title.getIconImage();
		if (null != img) {
			w = img.getWidth(this);
			h = img.getHeight(this);
		} else if (null != icon) {
			w = icon.getIconWidth();
			h = icon.getIconHeight();
		}
		size.width = Math.max(w, size.width);
		size.height = h;
		return size;
	}

	public void paintComponent(Graphics g) {
		Image img = title.getIconImage();
		if (null != img) {
			int y = (getHeight() - img.getHeight(this)) / 2;
			g.drawImage(img, 0, y, this);
			return;
		}
		int y = (getHeight() - icon.getIconHeight()) / 2;
		icon.paintIcon(this, g, 0, y);
	}
}
